package domain;

import java.util.Date;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;

import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Access(AccessType.PROPERTY)
@Table(indexes = {@Index(columnList = "amount")})
public class Bid extends DomainEntity {
	
	//Constructors

	public Bid() {
		super();
		
	}
	
	//Attributes
	private Date date;
	private Money amount;
	
	@NotNull
	@Past
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	
	@Valid
	@NotNull
	public Money getAmount() {
		return amount;
	}

	public void setAmount(Money amount) {
		this.amount = amount;
	}
	
	// Relationships//
	private Customer customer;
	private Auction auction;
	private Auction winnedAuction;
	
	@NotNull
	@Valid
	@ManyToOne(optional = false)
	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
	@NotNull
	@Valid
	@ManyToOne(optional = false)
	public Auction getAuction() {
		return auction;
	}

	public void setAuction(Auction auction) {
		this.auction = auction;
	}
	
	@Valid
	@OneToOne(mappedBy = "winningBid", optional = true)
	public Auction getWinnedAuction() {
		return winnedAuction;
	}
	public void setWinnedAuction(Auction winnedAuction) {
		this.winnedAuction = winnedAuction;
	}


}
